<template>
  <view>
    <view class="goods-cart" v-if="cart.length !== 0">
      <my-address></my-address>
      <view class="cart-title">
        <uni-icons type="shop" size="18"></uni-icons>
        <text>购物车</text>
      </view>
      <uni-swipe-action>
        <block class="goods-item" v-for="(goods,i) in cart" :key="i">
          <uni-swipe-action-item :options="options" @click="swipeActionClickHandler(goods)">
            <my-goods :goods="goods" :show-radio="true" @radioChange="radioChangeHandler(goods)" :showNum="true"
              @numChange="numChangeHandler"></my-goods>
          </uni-swipe-action-item>
        </block>
      </uni-swipe-action>
      <my-settle></my-settle>
    </view>


    <!-- 空购物车 -->
    <view class="empty-cart" v-else>
      <image src="/static/cart_empty@2x.png" class="empty-img"></image>
      <text class="tip-text">空空如也</text>
    </view>
  </view>

</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'

  export default {

    // 引入state在这里引入
    computed: {
      ...mapState("m_cart", ['cart']),
    },
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [badgeMix],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#f00'
          }
        }],
      };
    },
    methods: {
      // 引入方法,一定要在这里引入
      ...mapMutations("m_cart", ['updateStatus', 'updateGoodsCount', 'removeCartByGoodsId']),
      // 1. 点击单选按钮事件处理
      radioChangeHandler(e) {
        // 1.1 点击后选中状态取反，如：选中前true,再点击就变成false
        e.goods_state = !e.goods_state
        // 1.2 调用更新状态方法进行持久化保存状态
        this.updateStatus(e)
      },
      // 修改商品数量时触发的事件
      numChangeHandler(e) {
        console.log("cart:", e)
        // 保存修改的数量到本地
        this.updateGoodsCount(e)
      },
      // 右滑动删除事件
      swipeActionClickHandler(goods) {
        // console.log(goods)
        this.removeCartByGoodsId(goods)
      }
    }
  }
</script>

<style lang="scss">
  .goods-cart {
    padding-bottom: 50px;
  }

  .cart-title {
    display: flex;
    height: 40px;
    font-size: 14px;
    align-items: center;
    border-bottom: 1px solid #efefef;

    .uni-icons {
      padding: 0px;
      margin-left: 4px;
    }

    text {
      margin-left: 6px;
    }
  }

  .goods-item {}

  .empty-cart{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top:150px;
    .empty-img {
      width: 90px;
      height: 90px;
    }

    .tip-text {
      font-size: 12px;
      color: gray;
      margin-top: 15px;
    }
  }
</style>
